package org.mtmi.ui.scenes;

import org.mtmi.ui.actions.IAction;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.scenes.actions.FirstSceneAction;
import org.mtmi.ui.scenes.actions.LastSceneAction;
import org.mtmi.ui.scenes.actions.NextSceneAction;
import org.mtmi.ui.scenes.actions.PrevSceneAction;
import org.mtmi.ui.scenes.actions.ToggleEditSceneModeAction;

/**
 * Centralize actions that can be associated to {@link GlobalSceneSideActionButton}.
 * <p>
 * By default this class provides basic actions, like next/prev scene ...
 * <p>
 * TODO: this factory should support to addd subfactories implementing {@link ISideButtonActionProvider}
 */
public class SideButtonActionFactory implements ISideButtonActionProvider
{
	private static final IAction[] basics = new IAction[] {
		new NextSceneAction(),
		new PrevSceneAction(),
		new FirstSceneAction(),
		new LastSceneAction(),
		new ToggleEditSceneModeAction(),
	};
	
	private static final String[] action_ids;
	
	static {
		action_ids = new String[ basics.length ];
		for( int i=0; i<basics.length; i++ ) {
			action_ids[i] = basics[i].getClass().getName();
		}
	}
	
	private static SideButtonActionFactory instance;
	
	public static SideButtonActionFactory getDefault() {
		if( instance==null ) {
			instance = new SideButtonActionFactory();
		}
		return instance;
	}
	
	private SideButtonActionFactory() {}
	
	
	
	@Override
	public String[] getActionIds() {
		return action_ids;
	}

	private IAction getAction( String action_id ) {
		for( int i=0; i<action_ids.length; i++ ) {
			if( action_ids[i].equals( action_id ) ) {
				return basics[i];
			}
		}
		return null;
	}
	
	@Override
	public IDrawable getImage( Object element ) {
		if( !(element instanceof String ) ) return null;
		String action_id = (String)element;
		IAction action = getAction( action_id );
		if( action!=null ) {
			return action.getImage();
		}
		return null;
	}

	@Override
	public String getText( Object element ) {
		if( !(element instanceof String ) ) return null;
		String action_id = (String)element;
		IAction action = getAction( action_id );
		if( element instanceof ToggleEditSceneModeAction ) {
			//because I don't like action simple text 
			return "Toggle Play/Edit Scene";
		}
		if( action!=null ) {
			return action.getText();
		}
		return null;
	}
	
	@Override
	public IAction createAction( String action_id ) {
		IAction a = getAction( action_id );
		if( a==null ) {
			throw new Error("can't create action from id: "+action_id); //$NON-NLS-1$
		}
		return a;
	}
}
